<?php
namespace Admin\Controller;
use Admin\Controller\AdminController;
class CategoryController extends AdminController {

    //商品分类加载
    Public function category ($pid=0)
    {

        $this->assign('title','商品分类');
        $cate = A("Category","Event");
        $catelist = $cate -> category($pid);

        $this->assign('catelist',$catelist);
        $this->display('Category/category');
    }

    // 商品顶级分类添加 加载页面
    public function addTopCategory ()
    {
        $this->assign('title','商品顶级分类添加');

        $this->display('Category/addTopCategory');
    }

    // 商品顶级分类添加 向数据库中添加
    public function addTopCategoryDo ()
    {
        $add = A("Category","Event");
        $id = $add -> addTopCategory();//返回自增id

        if($id > 0){
            $this->success("商品分类添加成功","category");
        }
    }

    // 商品子分类添加 加载页面
    public function addCategory ()
    {
        $this->assign('title','商品子分类添加');
        $this->assign("pid",I("get.id"));
        //查询该分类的path
        $add = A("Category","Event");
        $path = $add -> queryfield();

        $this->assign("path",$path['path']);

        $this->display('Category/addCategory');
    }

    //查询分类的子类
    Public function queryCategory ()
    {
        $cate = A("Category","Event");
        $catelist = $cate -> queryCategory();
        echo $catelist;
    }

    ////从商品子分类返回父类,查询父类的信息
    Public function queryBackCategory ()
    {
        $cate = A("Category","Event");
        $catelist = $cate -> backCategory();
        echo $catelist;

    }

    //编辑分类页面
    Public function editCategory ()
    {
        $this->assign('title','分类编辑');
        //分类id
        $this->assign("id",I("get.id"));

        //查询该分类的pid path name
        $add = A("Category","Event");
        $cateinfo = $add -> queryfield();
        $this->assign("pid",$cateinfo['pid']);
        $this->assign("name",$cateinfo['name']);

        $this->display('Category/editCategory');
    }

    //编辑分类名称
    public function editCategoryDo ()
    {
        if(IS_POST){
            $edit = A("Category","Event");
            $res = $edit -> editCategoryDo();
            $pid = I("post.pid");
            if($res){
                $this->category($pid);
            }else{
                $this->success("修改失败");
            }
        }
    }

    //删除分类
    public function delCategory(){
        if(IS_GET){
            $del = A("Category","Event");
            $res = $del -> delCategoryDo();
            echo $res;
        }
    }

    //商品分类是否显示切换
    Public function change_category_show()
    {
        if(IS_GET){
            $goods = A("Category","Event");
            $res = $goods -> change_category_show();
            echo $res;
        }
    }

}